#  -*- coding: UTF-8 -*-

# Pycharm
# Python
# Powered By AUTUMNの窝
# 网址：https://yy47068186.yicp.fun
# 本项目基于 GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007

from pinpong.extension.unihiker import *
from pinpong.board import SR04_URM10
from pinpong.board import Board,Pin
from pinpong.board import DHT11
from pinpong.board import Board
from unihiker import GUI
import siot
import time

# 事件回调函数
def on_message_callback(client, userdata, msg):
    if (msg.topic == "siot/led"):
        if (msg.payload.decode() == "打开红色的灯"):
            p_p5_out=Pin(Pin.P5, Pin.OUT)
            p_p5_out.write_digital(1)
        if (msg.payload.decode() == "打开黄色的灯"):
            p_p6_out=Pin(Pin.P6, Pin.OUT)
            p_p6_out.write_digital(1)
        if (msg.payload.decode() == "打开绿色的灯"):
            p_p7_out=Pin(Pin.P7, Pin.OUT)
            p_p7_out.write_digital(1)
        if (msg.payload.decode() == "关闭红色的灯"):
            p_p5_out=Pin(Pin.P5, Pin.OUT)
            p_p5_out.write_digital(0)
        if (msg.payload.decode() == "关闭黄色的灯"):
            p_p6_out=Pin(Pin.P6, Pin.OUT)
            p_p6_out.write_digital(0)
        if (msg.payload.decode() == "关闭绿色的灯"):
            p_p7_out=Pin(Pin.P7, Pin.OUT)
            p_p7_out.write_digital(0)
        if (msg.payload.decode() == "全部打开"):
            p_p5_out=Pin(Pin.P5, Pin.OUT)
            p_p5_out.write_digital(1)
            p_p6_out=Pin(Pin.P6, Pin.OUT)
            p_p6_out.write_digital(1)
            p_p7_out=Pin(Pin.P7, Pin.OUT)
            p_p7_out.write_digital(1)
        if (msg.payload.decode() == "全部关闭"):
            p_p5_out=Pin(Pin.P5, Pin.OUT)
            p_p5_out.write_digital(0)
            p_p6_out=Pin(Pin.P6, Pin.OUT)
            p_p6_out.write_digital(0)
            p_p7_out=Pin(Pin.P7, Pin.OUT)
            p_p7_out.write_digital(0)
    if (msg.topic == "siot/song"):
        if (msg.payload.decode() == "彩虹猫"):
            buzzer.play(buzzer.NYAN,buzzer.Once)
        if (msg.payload.decode() == "生日歌"):
            buzzer.play(buzzer.BIRTHDAY,buzzer.Once)
        if (msg.payload.decode() == "手机铃声"):
            buzzer.play(buzzer.RINGTONE,buzzer.Once)
        if (msg.payload.decode() == "关闭所有音乐"):
            p_p5_out=Pin(Pin.P5, Pin.OUT)
            p_p5_out.write_digital(0)
            p_p6_out=Pin(Pin.P6, Pin.OUT)
            p_p6_out.write_digital(0)
            p_p7_out=Pin(Pin.P7, Pin.OUT)
            p_p7_out.write_digital(0)
    if (msg.topic == "siot/dht11"):
        if (msg.payload.decode() == "当前温度"):
            siot.publish(topic="siot/dht11", data=(str("当前温度：") + str((str(dht1.temp_c()) + str("°C")))))
        if (msg.payload.decode() == "当前湿度"):
            siot.publish(topic="siot/dht11", data=(str("当前温度：") + str((str(dht1.humidity()) + str("%rh")))))
    if (msg.topic == "siot/radar"):
        DJ = SR04_URM10(Pin((Pin.P20)),Pin((Pin.P19)))
        my_variable = DJ.distance_cm()
        if (msg.payload.decode() == "距离"):
            siot.publish(topic="siot/radar", data=(str("超声波传感器：") + str((str(my_variable) + str("(cm)")))))
    


Board().begin()
siot.init(client_id="09272369677451864",server="127.0.0.1",port=1883,user="siot",password="dfrobot")
u_gui=GUI()
siot.set_callback(on_message_callback)
p_p10_analog=Pin(Pin.P10, Pin.ANALOG)
siot.connect()
siot.loop()
siot.getsubscribe(topic="siot/led")
siot.publish(topic="siot/led", data="传感器连接成功")
siot.getsubscribe(topic="siot/song")
siot.publish(topic="siot/song", data="传感器连接成功")
siot.getsubscribe(topic="siot/dht11")
siot.publish(topic="siot/dht11", data="传感器连接成功")
siot.getsubscribe(topic="siot/radar")
siot.publish(topic="siot/radar", data="传感器连接成功")
siot.getsubscribe(topic="siot/Client")
dht1 = DHT11(Pin((Pin.P11)))
text=u_gui.draw_text(text="欢迎使用",x=0,y=0,font_size=20, color="#000000")
text_1=u_gui.draw_text(text="AUTUMN物联网",x=0,y=25,font_size=20, color="#000000")
img=u_gui.draw_image(image="about.jpg",x=0,y=60)

while True:
    pass